package com.zzy.util.testfile;

import java.io.*;
import java.nio.file.Files;

public class TestFile {
    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        test4();
    }


    public static void test1(){
        InputStream in = null;
        OutputStream out = null;
        String s = TestFile.class.getResource("/").getPath();
        System.out.println(s);
        try {
            File file = new File(s + "aa.txt");
            in = new FileInputStream(file);

            File file1 = new File(s + "bb.txt");
            if(!file1.exists()){
                file1.createNewFile();
            }
            out = new FileOutputStream(file1);

            Reader reader = new InputStreamReader(in,"utf-8");
            BufferedReader br = new BufferedReader(reader);

            Writer writer = new OutputStreamWriter(out,"utf-8");
            BufferedWriter bw = new BufferedWriter(writer);

            String res = "";
            while (  (res=br.readLine()) !=null ){
                System.out.println(res);
                bw.write(res);
            }
            bw.flush();

            bw.close();
            writer.close();

            br.close();
            reader.close();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


    public static void test2(){
        InputStream in = null;
        OutputStream out = null;
        String s = TestFile.class.getResource("/").getPath();
        try {
            File file = new File(s + "aa.txt");
            in = new FileInputStream(file);

            File file1 = new File(s + "cc.txt");
            if(!file1.exists()){
                file1.createNewFile();
            }
            out = new FileOutputStream(file1);

            byte buffer[] = new byte[1024];
            int leng = 0;
            while (  (leng = in.read(buffer,0,buffer.length)) !=-1  ){
               out.write(buffer,0,leng);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void test3(){
        BufferedInputStream bin = null;
        InputStream in = null;

        BufferedOutputStream bout = null;
        OutputStream out = null;

        String s = TestFile.class.getResource("/").getPath();
        try {
            File file = new File(s + "aa.txt");

            in = new FileInputStream(file);
            bin = new BufferedInputStream(in);

            File file1 = new File(s + "ee.txt");
            if(!file1.exists()){
                file1.createNewFile();
            }
            out = new FileOutputStream(file1);
            bout = new BufferedOutputStream(out);

            byte buffer[] = new byte[1024];
            int leng = 0;
            while (  (leng = bin.read(buffer,0,buffer.length)) !=-1  ){
                bout.write(buffer,0,leng);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                bin.close();
                bout.close();
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


    public static void test4(){
        String s = TestFile.class.getResource("/").getPath();
        try {
            File file = new File(s + "aa.txt");
            File file2 = new File(s + "nn.txt");
            Files.copy(file.toPath(),file2.toPath());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
